package com.xingfly.xfauth.endpoint;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.TokenRequest;
import org.springframework.security.oauth2.provider.endpoint.FrameworkEndpoint;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@FrameworkEndpoint
public class RefreshAccessTokenEndpoint {
    @Autowired
    @Qualifier("defaultAuthorizationServerTokenServices")
    AuthorizationServerTokenServices defaultAuthorizationServerTokenServices;

    @RequestMapping(method = RequestMethod.PUT, value = "/oauth/token")
    @ResponseBody
    public OAuth2AccessToken refreshAccessToken(String refresh_token) throws AuthenticationException {
        Map<String, String> requestParameters = new HashMap<String, String>();
        String clientId = "android";
        Collection<String> scope = new ArrayList<String>();
        scope.add("xx");
        String grantType = "password";
        TokenRequest tokenRequest = new TokenRequest(requestParameters, clientId, scope, grantType);
        return defaultAuthorizationServerTokenServices.refreshAccessToken(refresh_token, tokenRequest);
    }
}
